package com.yan.controller;

import java.util.Date;
import java.util.List;

import com.yan.controller.viewModel.user.BorrowInfo;
import com.yan.controller.viewModel.user.ReserveInfo;
import com.yan.controller.viewModel.user.SuggestBuyInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.yan.service.AdminService;
import com.yan.vo.Page;

@Controller
@RequestMapping("/admin")
public class AdminController {
	@Autowired
	private AdminService adminService;

	@RequestMapping("/index")
	public String toIndex() {
		return "/admin/index";
	}

	/**
	 * 查询借书表并跳转到借书查询页面 
	 */
	@RequestMapping("/show")
	public String toShow(Model model, BorrowInfo borrow) {

		if (borrow.getSid() != null) {
			model.addAttribute("sid", borrow.getSid());
		}

		if (!"".equals(borrow.getUname()) || borrow.getUname().length() > 0) {
			model.addAttribute("uname", borrow.getUname());
		}
		
		Page page = adminService.getBorrows(borrow);
		
		model.addAttribute("page", page);
		model.addAttribute("currentPage", borrow.getCurrentPage());
		model.addAttribute("totalPage", page.getTotalPage());
		return "/admin/show";
	}

	/**
	 * 借书审批通过
	 */
	@RequestMapping("/allow")
	public String allowBorrows(Integer id) {
		// 审批通过将借书表中的status设置为1
		//并增加未归还书籍
		adminService.updateBorrows(id);
		adminService.inserBackBooks(id);
		// 书籍数目减1
		int booksId = adminService.selectBooksId(id);
		adminService.descBooks(booksId);
		return "redirect:/admin/show";
	}
	
   /**
   * 借书审批不通过 
   */
	@RequestMapping("/notAllow")
	public String notAllow(Integer id) {
		//将借书表中的status改为2
		adminService.notAllowBorrows(id);
		// 更新结束时间
		Date endTime = new Date();
		BorrowInfo borrow = new BorrowInfo();
		borrow.setId(id);
		borrow.setEndTime(endTime);
		adminService.updateBorrowsTime(borrow);
		return "redirect:/admin/show";
	}

	/**
	 * 查询归还表并跳转到处理归还页面
	 */
	@RequestMapping("/backBooks")
	public String getBackBooks(Model model,BorrowInfo borrow) {

		Page page = adminService.getBackBooks(borrow);
		model.addAttribute("page", page);
		model.addAttribute("currentPage", borrow.getCurrentPage());
		model.addAttribute("totalPage", page.getTotalPage());
		
		return "/admin/showBackBook";
	}

	/*
	 * 确认归还图书
	 */
	@RequestMapping("/allowBackBook")
	public String allowBackBook(Integer id) {
		
		adminService.allowBackBook(id);//将还书表中此书的状态设置为2
		int borrowsId = adminService.selectBorrowsId(id);//获得借书表中此书的id
		int booksId = adminService.selectBooksId(borrowsId);//获得图书表中此书的id
		// 书籍数目加1
		adminService.updateBooks(booksId);
		// 更新归还时间
		Date endTime = new Date();
		BorrowInfo borrow = new BorrowInfo();
		borrow.setId(borrowsId);
		borrow.setEndTime(endTime);
		adminService.updateBorrowsTime(borrow);
		return "redirect:/admin/backBooks";
	}
	

	// 获得所有预定书籍信息
	@RequestMapping("/showReserveBook")
	public String showReserveBook(Model model) {
		List<ReserveInfo> allReserves = adminService.getAllReserve();
		model.addAttribute("allReserves", allReserves);
		return "/admin/showReserveBook";
	}

	// 允许预定书籍信息
	@RequestMapping("/allowReserveBook")
	public String allowReserveBook(Integer id) {
		adminService.updateReserveBook(id);//将预定表中此书的状态改为1
		// 预定处理时间
		Date endTime = new Date();
		ReserveInfo reserve = new ReserveInfo();
		reserve.setId(id);
		reserve.setEndTime(endTime);
		adminService.updateReserveTime(reserve);
		return "redirect:/admin/showReserveBook";
	}

	// 不允许预定书籍信息
	@RequestMapping("/notAllowReserveBook")
	public String notAllowReserveBook(Integer id) {
		adminService.notAllowReserveBook(id);//将预定表中此书的状态改为2
		Date endTime = new Date();
		ReserveInfo reserve = new ReserveInfo();
		reserve.setId(id);
		reserve.setEndTime(endTime);
		adminService.updateReserveTime(reserve);
		return "redirect:/admin/showReserveBook";
	}
	
	//跳转到建购处理页面
	@RequestMapping("/showSuggestBuy")
	public String getSuggestBuy(Model model, SuggestBuyInfo suggestBuy) {

		Page page = adminService.getSuggestBuys(suggestBuy);

		model.addAttribute("page", page);
		
		model.addAttribute("currentPage", suggestBuy.getCurrentPage());
		model.addAttribute("totalPage", page.getTotalPage());

		return "/admin/showSuggestBuy";
	}

	//允许购买
	@RequestMapping("/allowBuy")
	public String allowBuy(int id) {
		adminService.allowBuy(id);
		return "redirect:/admin/showSuggestBuy";
	}
	
	//不允许购买
	@RequestMapping("/notAllowBuy")
	public String notAllowBuy(int id) {
		adminService.notAllowBuy(id);
		return "redirect:/admin/showSuggestBuy";
	}
	
	//查询用户信息
	@RequestMapping("/showUsers")
	public String showUsers(BorrowInfo borrow,Model model) {
		
	
		if (!"".equals(borrow.getUname()) || borrow.getUname().length() > 0) {
			model.addAttribute("uname", borrow.getUname());
		}
		
		Page page = adminService.getAllPersonMessage(borrow);
		
		if(page.getList().size()==0) {
			model.addAttribute("msg", "查不到指定用户，请确保该用户的信息填写完整！");
		}
		
		model.addAttribute("page", page);
		model.addAttribute("currentPage", borrow.getCurrentPage());
		model.addAttribute("totalPage", page.getTotalPage());
		
		
		return "/admin/showUsers";
	}
	
	//查询用户的借书情况
	@RequestMapping("/showUserBorrows")
	public String showUserBorrows(BorrowInfo borrow,Model model) {
        Page page = adminService.getUserBorrows(borrow);
        model.addAttribute("borrow",borrow);
		model.addAttribute("page", page);
		model.addAttribute("currentPage", borrow.getCurrentPage());
		model.addAttribute("totalPage", page.getTotalPage());
		
		return "/admin/ShowUserBorrows";
	}
	
}
